<template>
  <div class="login-bg">
    <div class="login-card">
      <div class="login-title">找回密码</div>
      <div class="login-subtitle">通过手机号验证重置密码</div>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-row :gutter="10">
            <el-col :span="14">
              <el-input v-model="form.code" placeholder="请输入验证码" />
            </el-col>
            <el-col :span="10">
              <el-button :disabled="countdown > 0 || sending" @click="sendCode">
                {{ countdown > 0 ? `${countdown}s后重试` : '获取验证码' }}
              </el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="form.newPassword" type="password" placeholder="请输入新密码" show-password prefix-icon="Lock" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReset" :loading="loading" style="width: 100%">重置密码</el-button>
        </el-form-item>
      </el-form>
      <div class="login-footer">
        <el-link type="info" @click="goToLogin">返回登录</el-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import userAPI from '@/api/user'

const router = useRouter()
const formRef = ref()
const loading = ref(false)
const sending = ref(false)
const countdown = ref(0)
let timer = null

const form = reactive({
  account: '',
  phone: '',
  code: '',
  newPassword: ''
})

const rules = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const sendCode = async () => {
  if (!form.phone || !/^1[3-9]\d{9}$/.test(form.phone)) {
    ElMessage.error('请输入正确的手机号')
    return
  }
  sending.value = true
  try {
    const res = await userAPI.sendResetCode({ account: form.account, phone: form.phone })
    if (res.code === 200) {
      ElMessage.success('验证码已发送')
      countdown.value = 60
      timer = setInterval(() => {
        countdown.value--
        if (countdown.value <= 0) {
          clearInterval(timer)
        }
      }, 1000)
    } else {
      ElMessage.error(res.msg || '验证码发送失败')
    }
  } finally {
    sending.value = false
  }
}

const handleReset = async () => {
  await formRef.value.validate()
  loading.value = true
  try {
    const res = await userAPI.resetPassword({
      account: form.account,
      phone: form.phone,
      code: form.code,
      newPassword: form.newPassword
    })
    if (res.code === 200) {
      ElMessage.success('密码重置成功，请登录')
      router.push('/login')
    } else {
      ElMessage.error(res.msg || '重置失败')
    }
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #e0ffd6 0%, #b2f7ef 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  width: 400px;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.12);
  padding: 40px 36px 32px 36px;
  display: flex;
  flex-direction: column;
  align-items: center;
  animation: fadeIn 0.8s;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(40px);}
  to { opacity: 1; transform: translateY(0);}
}
.login-title {
  font-size: 2rem;
  font-weight: 700;
  color: #222;
  margin-bottom: 12px;
  letter-spacing: 2px;
  text-align: center;
}
.login-subtitle {
  font-size: 1rem;
  color: #888;
  margin-bottom: 24px;
  text-align: center;
}
.el-form {
  width: 100%;
}
.el-input__wrapper, .el-input {
  border-radius: 8px !important;
}
.el-button {
  border-radius: 8px;
  font-size: 16px;
  height: 44px;
}
.el-button.is-primary {
  background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
  border: none;
}
.login-footer {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  margin-top: 18px;
  font-size: 15px;
}
.login-footer .el-link {
  font-size: 15px;
}
@media (max-width: 600px) {
  .login-card {
    width: 95vw;
    padding: 24px 8px 16px 8px;
  }
  .login-title {
    font-size: 1.3rem;
  }
}
</style>
